﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Web.Configuration;
using System.Xml;
using SocialFront.Providers;
using SocialFront.Providers.Document;
using SocialFront.Providers.Member;
using SocialFront.Providers.User;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.relation;
using umbraco.cms.businesslogic.template;
using umbraco.cms.businesslogic.web;
using umbraco.presentation.nodeFactory;

namespace SocialFront.Provider.Umbraco
{
    public class UmbracoDocument : DocumentBase
    {
        public override string MakeMembersArea(Entities.Member sfMember)
        {
            User systemUser = UserProvider.Provider.GetSystemUser();

            // Create a members area node in the content tree for the member
            Document membersArea = Document.MakeNew(sfMember.LoginName, DocumentType.GetByAlias("sfMember"),
                             systemUser, Config.sfMembersNodeId);

            // Relate the new node to the member
            Relation.MakeNew(sfMember.Id, membersArea.Id, RelationType.GetByAlias("relateMemberToMemberDocType"),
                             String.Format("Relating content folder {0} to member {1}", membersArea.Text,
                                           sfMember.LoginName));

            // Create a blog node in this new member's area for the member
            Document membersBlog = Document.MakeNew("Blog", DocumentType.GetByAlias("sfBlog"),
                                                    systemUser, membersArea.Id);

            // Relate the new node to the member
            Relation.MakeNew(sfMember.Id, membersBlog.Id, RelationType.GetByAlias("relateMemberToBlogDocType"),
                             String.Format("Relating blog folder {0} to member {1}", membersBlog.Id,
                                           sfMember.LoginName));

            // Create a gallery node in this new member's area for the member
            Document membersGallery = Document.MakeNew("Gallery", DocumentType.GetByAlias("sfGallery"),
                                                    systemUser, membersArea.Id);

            // Relate the new node to the member
            Relation.MakeNew(sfMember.Id, membersGallery.Id, RelationType.GetByAlias("relateMemberToGalleryDocType"),
                             String.Format("Relating gallery folder {0} to member {1}", membersGallery.Id,
                                           sfMember.LoginName));

            // Publish the new folders, setting blog and gallery to enabled = false
            membersArea.getProperty("pageTitle").Value = String.Format("{0}'s area", sfMember.LoginName);
            membersArea.Save();
            membersArea.Publish(systemUser);
            umbraco.library.UpdateDocumentCache(membersArea.Id);
            string memberUrl = umbraco.library.NiceUrl(membersArea.Id);

            membersBlog.getProperty("pageTitle").Value = String.Format("{0}'s blog", sfMember.LoginName);
            membersBlog.getProperty("title").Value = String.Format("{0}'s blog", sfMember.LoginName);
            membersBlog.getProperty("enabled").Value = false;
            membersBlog.Save();
            membersBlog.Publish(systemUser);
            umbraco.library.UpdateDocumentCache(membersBlog.Id);

            membersGallery.getProperty("pageTitle").Value = String.Format("{0}'s gallery", sfMember.LoginName);
            membersGallery.getProperty("title").Value = String.Format("{0}'s gallery", sfMember.LoginName);
            membersGallery.getProperty("enabled").Value = false;
            membersGallery.Save();
            membersGallery.Publish(systemUser);
            umbraco.library.UpdateDocumentCache(membersGallery.Id);

            return memberUrl;
        }

        public override int GetCurrentPage()
        {
            return Node.GetCurrent().Id;
        }

        public override string GetPageUrl(int id)
        {
            return umbraco.library.NiceUrl(id);
        }
    }
}